package pl.msikora.android.lapmagic.database;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PointsTable {
	
	private static final String TAG = "PointsTable";
	
	public static final String TABLE_POINTS = "points";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_TRACK_ID = "track_id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_TYPE = "type";
	public static final String COLUMN_LATITUDE = "latitude";
	public static final String COLUMN_LONGITUDE = "longitude";
	public static final String COLUMN_RADIUS = "radius";
	
	private static final String TABLE_CREATE = "create table "
			+ TABLE_POINTS
			+ "(" 
			+ COLUMN_ID + " integer primary key autoincrement, "
			+ COLUMN_TRACK_ID + " integer not null, " // references tracks on update and delete cascade
			+ COLUMN_NAME + " text default 'split 1', "
			+ COLUMN_TYPE + " text check(" + COLUMN_TYPE 
			+ " in ('start', 'split')), "
			+ COLUMN_LATITUDE + " integer not null, " // latitudeE6
			+ COLUMN_LONGITUDE + " integer not null, " // longitudeE6
			+ COLUMN_RADIUS + " integer default '15', "
			+ "unique(" + COLUMN_TRACK_ID + ", " + COLUMN_NAME + ")"
			+ ");";
	
	public static void onCreate(SQLiteDatabase database) {
		Log.d(TAG, "creating table points with statement '" + TABLE_CREATE
				+ "'");
		database.execSQL(TABLE_CREATE);
	}
	
	public static void onUpgrade(SQLiteDatabase database, int oldVersion, 
			int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion
				+ " to " + newVersion 
				+ ", which will destroy new all old data");
		database.execSQL("drop table if exists " + TABLE_POINTS);
		onCreate(database);
	}

}
